#include "ipsettingdialog.h"
#include "ui_ipsettingdialog.h"

IpSettingDialog::IpSettingDialog(int styleId,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::IpSettingDialog)
{
    ui->setupUi(this);
    move(0,0);
    initialDisplayStyle(styleId);
//#ifdef BUILD_ARM
//    setGeometry(70,128,460,595);
//#endif

    ui->modifyHmi_Btn->setText(tr("修改"));
    ui->modifyController_Btn->setText(tr("修改"));

    //init
    initReadFileFlag = false;

    for(int i = 0;i<4;i++)
    {
        hmiIp.append(0);
        hmiGateway.append(0);
        hmiDNS.append(0);

        controllerIp.append(0);
        controllerGateway.append(0);
        controllerDNS.append(0);
    }

    hmiIpLine.append(ui->hmi1_lineEdit);
    hmiIpLine.append(ui->hmi2_lineEdit);
    hmiIpLine.append(ui->hmi3_lineEdit);
    hmiIpLine.append(ui->hmi4_lineEdit);

    hmiGatewayLine.append(ui->hmi1_lineEdit_3);
    hmiGatewayLine.append(ui->hmi2_lineEdit_3);
    hmiGatewayLine.append(ui->hmi3_lineEdit_3);
    hmiGatewayLine.append(ui->hmi4_lineEdit_3);

    hmiDNSLine.append(ui->hmi1_lineEdit_5);
    hmiDNSLine.append(ui->hmi2_lineEdit_5);
    hmiDNSLine.append(ui->hmi3_lineEdit_5);
    hmiDNSLine.append(ui->hmi4_lineEdit_5);

    controllerIpLine.append(ui->controller1_lineEdit);
    controllerIpLine.append(ui->controller2_lineEdit);
    controllerIpLine.append(ui->controller3_lineEdit);
    controllerIpLine.append(ui->controller4_lineEdit);

    controllerGatewayLine.append(ui->controller1_lineEdit_2);
    controllerGatewayLine.append(ui->controller2_lineEdit_2);
    controllerGatewayLine.append(ui->controller3_lineEdit_2);
    controllerGatewayLine.append(ui->controller4_lineEdit_2);

    controllerDNSLine.append(ui->controller1_lineEdit_3);
    controllerDNSLine.append(ui->controller2_lineEdit_3);
    controllerDNSLine.append(ui->controller3_lineEdit_3);
    controllerDNSLine.append(ui->controller4_lineEdit_3);
}

void IpSettingDialog::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);
        ui->returnBtn->setGeometry(500,400,70,40);
        break;
    default:
        break;
    }
}

IpSettingDialog::~IpSettingDialog()
{
    delete ui;
}

void IpSettingDialog::initReadFileHmi(QString ip,QString gateway,QString dns)
{
    QStringList list = ip.split(".");
    if(list.size() == hmiIp.size())
    {
        for(int i = 0;i<list.size();i++)
        {
            hmiIp[i] = list[i].toInt();
        }
    }
    list.clear();
    list = gateway.split(".");
    if(list.size() == hmiGateway.size())
    {
        for(int i = 0;i<list.size();i++)
        {
            hmiGateway[i] = list[i].toInt();
        }
    }
    list.clear();
    list = dns.split(".");
    if(list.size() == hmiDNS.size())
    {
        for(int i = 0;i<list.size();i++)
        {
            hmiDNS[i] = list[i].toInt();
        }
    }
}

void IpSettingDialog::initReadFileCtrl(QString ip,QString gateway,QString dns)
{
    QStringList list = ip.split(".");
    if(list.size() == controllerIp.size())
    {
        for(int i = 0;i<list.size();i++)
        {
            controllerIp[i] = list[i].toInt();
        }
    }
    list.clear();
    list = gateway.split(".");
    if(list.size() == controllerGateway.size())
    {
        for(int i = 0;i<list.size();i++)
        {
            controllerGateway[i] = list[i].toInt();
        }
    }
    list.clear();
    list = dns.split(".");
    if(list.size() == controllerDNS.size())
    {
        for(int i = 0;i<list.size();i++)
        {
            controllerDNS[i] = list[i].toInt();
        }
    }
}

void IpSettingDialog::changeLanguage()
{
    ui->retranslateUi(this);
}

void IpSettingDialog::updateView()
{    
    for(int i = 0;i<hmiIp.size();i++)
    {
        hmiIpLine[i]->setText(QString::number(hmiIp[i]));
        hmiGatewayLine[i]->setText(QString::number(hmiGateway[i]));
        hmiDNSLine[i]->setText(QString::number(hmiDNS[i]));

        controllerIpLine[i]->setText(QString::number(controllerIp[i]));
        controllerGatewayLine[i]->setText(QString::number(controllerGateway[i]));
        controllerDNSLine[i]->setText(QString::number(controllerDNS[i]));
    }
}

void IpSettingDialog::getInput()
{
    for(int i= 0;i<4;i++)
    {
        //hmi
        hmiIp[i] = hmiIpLine[i]->text().toInt();
        hmiGateway[i] = hmiGatewayLine[i]->text().toInt();
        hmiDNS[i] = hmiDNSLine[i]->text().toInt();

        //ctrl
        controllerIp[i] = controllerIpLine[i]->text().toInt();
        controllerGateway[i] = controllerGatewayLine[i]->text().toInt();
        controllerDNS[i] = controllerDNSLine[i]->text().toInt();
    }
}

void IpSettingDialog::on_modifyHmi_Btn_clicked()
{
    getInput();
    emit setHmiIp(hmiIp,hmiGateway,hmiDNS);
}

void IpSettingDialog::on_modifyController_Btn_clicked()
{
    getInput();
    emit setControllerIp(controllerIp,controllerGateway,controllerDNS);
}

void IpSettingDialog::on_returnBtn_clicked()
{
    this->hide();
}
